package org.jeecg.modules.demo.staffgoods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.staffgoods.entity.StaffGoods;
import org.jeecg.modules.demo.staffgoods.service.IStaffGoodsService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 员工商品
 * @Author: jeecg-boot
 * @Date: 2021-12-30
 * @Version: V1.0
 */
@Api(tags = "员工商品")
@RestController
@RequestMapping("/staffgoods/staffGoods")
@Slf4j
public class StaffGoodsController extends JeecgController<StaffGoods, IStaffGoodsService> {
    @Autowired
    private IStaffGoodsService staffGoodsService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 分页列表查询
     *
     * @param staffGoods
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "员工商品-分页列表查询")
    @ApiOperation(value = "员工商品-分页列表查询", notes = "员工商品-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StaffGoods staffGoods,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        String staffId = staffGoods.getStaffId();
        if (oConvertUtils.isEmpty(staffId)) {
            return Result.error("员工ID必传参数");
        }
        QueryWrapper<StaffGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        QueryGenerator.initQueryWrapper(queryWrapper, staffGoods, req.getParameterMap());
        Page<StaffGoods> page = new Page<StaffGoods>(pageNo, pageSize);
        IPage<StaffGoods> pageList = staffGoodsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param staffGoods
     * @return
     */
    @AutoLog(value = "员工商品-添加")
    @ApiOperation(value = "员工商品-添加", notes = "员工商品-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StaffGoods staffGoods, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        String staffId = staffGoods.getStaffId();
        if (oConvertUtils.isEmpty(staffId)) {
            return Result.error("员工ID必传参数");
        }
        String stockholderId = staffGoods.getGoodsId();
        if (oConvertUtils.isEmpty(stockholderId)) {
            return Result.error("股东ID必传参数");
        }
        staffGoods.setStoreId(storeId);
        staffGoodsService.save(staffGoods);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param staffGoods
     * @return
     */
    @AutoLog(value = "员工商品-编辑")
    @ApiOperation(value = "员工商品-编辑", notes = "员工商品-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StaffGoods staffGoods) {
        staffGoodsService.updateById(staffGoods);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "员工商品-通过id删除")
    @ApiOperation(value = "员工商品-通过id删除", notes = "员工商品-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        staffGoodsService.removeById(id);
        return Result.OK("删除成功!");
    }
}
